package ar.com.PanteraRosa;

import java.util.ArrayList;

import org.json.JSONException;

import android.R.integer;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import ar.com.PanteraRosa.Business.ContactController;
import ar.com.PanteraRosa.Business.UserController;
import ar.com.PanteraRosa.Common.Conf;
import ar.com.PanteraRosa.Entity.User;
import ar.com.PanteraRosa.Utils.ListViewAdapter;
import ar.com.PanteraRosa.Utils.ListViewItem;

public class MyContactsUserActivity extends ListActivity {

    private ListViewAdapter listViewAdapter;
    private UserController userController;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.mycontacts);
	setTitle(R.string.app_name);

	userController = new UserController(this);
	ContactController contactController = new ContactController(this);

	userController.userDao.open();
	try {
	    contactController.updateContactsOnMobile(String.valueOf(Conf
		    .getInstance().getUserId()));
	    userController.updateUsersOnMobile(contactController
		    .getJsonDataContactJoinUser());
	} catch (JSONException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	}

	Cursor c = userController.loadAllUsersOnMobile();
	userController.userDao.cerrar();

	// Cursor notesCursor = c;
	// startManagingCursor(notesCursor);
	// Create an array to specify the fields we want to display in the list
	// (only TITLE)
	// String[] from = new String[]{ContactDao.KEY_ROWID};
	// and an array of the fields we want to bind those fields to (in this
	// case just text1)
	// int[] to = new int[]{R.id.title};
	// Now create a simple cursor adapter and set it to display
	// SimpleCursorAdapter notes = new SimpleCursorAdapter(this,
	// R.layout.elementolista, notesCursor, from, to);
	// setListAdapter(notes);

	User userAux;
	ArrayList<ListViewItem> items = new ArrayList<ListViewItem>();

	if (c.moveToFirst()) {
	    do {
		userAux = new User(Long.parseLong(c.getString(0)),
			c.getString(1), "", c.getString(2), c.getString(3),
			c.getString(4));

		//userController.insertUserOnCloud(userAux);
		items.add(new ListViewItem(userAux.getUserNick()));
	    } while (c.moveToNext());
	}

	listViewAdapter = new ListViewAdapter(this, R.layout.elementolista,
		items);
	setListAdapter(listViewAdapter);
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
	// TODO Auto-generated method stub
	super.onListItemClick(l, v, position, id);
	Log.e("Fede", "Has seleccionado el elemento " + position);
    }

    // public void btnBack_OnClick(View vista) {
    // Intent intent = new Intent();
    // intent.setClass(this, HomeUserActivity.class);
    // startActivity(intent);
    // }

    public void btnAddContact_OnClick(View vista) {
	Intent intent = new Intent();
	intent.setClass(this, AddContactUserActivity.class);
	startActivity(intent);
    }
}
